package com.group.itms.routes.utils;

import android.view.MotionEvent;

public class TapDetector {

	private float mStartX;
	private float mStartY;
	private float mThreshold = 30f;

	private OnTapListener mOnTapListener;

	public TapDetector(OnTapListener l) {
		mOnTapListener = l;
	}

	public void setThreshold(float threshold) {
		mThreshold = threshold;
	}

	public boolean onTouchEvent(MotionEvent event) {
		if (event.getPointerCount() == 1) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mStartX = event.getX(0);
				mStartY = event.getY(0);
				break;
			case MotionEvent.ACTION_UP:
				float x = event.getX(0);
				float y = event.getY(0);
				if (Math.hypot(x - mStartX, y - mStartY) < mThreshold) {
					return dispatchOnTap(x, y);
				}
//				break;
				return true;
			}
		}
		return false;
	}

	private boolean dispatchOnTap(float x, float y) {
		if (mOnTapListener != null) {
			return mOnTapListener.onTap(x, y);
		}
		return false;
	}

	public interface OnTapListener {
		boolean onTap(float x, float y);
	}

}
